﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Collections;

namespace DiaryPlanner.Diary
{
    public partial class new_diary : Form
    {
        public Diary_main object_diary_main;//Diary_main의 object를 생성
        private int align_value, emotion_value , weather_value;
        public string  all_date;
        public int check_photoOrdraw;

        public new_diary(Diary_main diary_main)
        {
            InitializeComponent();
            object_diary_main = diary_main;
            check_photoOrdraw = 0;
            init_new_diary();
        }

        /////////선택된 날짜정보를 받아온다
        private void init_new_diary()
        {
            
            select_year_text_box.Text = object_diary_main.load_year_value();
            select_month_text.Text = object_diary_main.load_month_value();
            select_day_text.Text = object_diary_main.load_day_value();
            align_value = 0;
            all_date = select_year_text_box.Text + select_month_text.Text + select_day_text.Text;
            emotion_value = 0;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            object_diary_main.write_text_value(this.new_daily_diary_text);
            object_diary_main.update_emotion(emotion_value , weather_value);
            save_db(this.new_daily_diary_text.Text, (select_year_text_box.Text + select_month_text.Text + select_day_text.Text));
            object_diary_main.update_view(all_date);
            this.Dispose();
            this.Close();
        }

        ////////////////////////////////////////////////////////////////
        ////////////////text의 alignment를 수정해주는 이벤트
        
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            this.new_daily_diary_text.TextAlign = System.Windows.Forms.HorizontalAlignment.Left;
            align_value = 0;
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            this.new_daily_diary_text.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            align_value = 1;
        }

        private void pictureBox3_Click(object sender, EventArgs e)
        {
            this.new_daily_diary_text.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            align_value = 2;
        }



        ////////////////////////////////////////////////////////////////
        //기분등의 상태를 나타내주는 것
        //날씨, 다른것으로 변경 가능

        private void pictureBox5_Click(object sender, EventArgs e)
        {
            
            this.button4.Visible = true;
            this.button5.Visible = true;
            this.button6.Visible = true;
            this.button7.Visible = true;
            this.pictureBox4.Visible = true;
        }


        //////////////////////////////////////////////////////////
        ////////////////////기분 (혹은 날씨 등을) 선택한후에 visible을 false로 변경
        private void button4_Click_1(object sender, EventArgs e)
        {//smile
            emotion_value = 0;
            this.emotion_pic_view.Image = global::DiaryPlanner.Properties.Resources.smile_big;
            this.button4.Visible = false;
            this.button5.Visible = false;
            this.button6.Visible = false;
            this.button7.Visible = false;
            this.pictureBox4.Visible = false;
        }

        private void button5_Click_1(object sender, EventArgs e)
        {//sad
            emotion_value = 1;
            this.emotion_pic_view.Image = global::DiaryPlanner.Properties.Resources.sad_big;
            this.button4.Visible = false;
            this.button5.Visible = false;
            this.button6.Visible = false;
            this.button7.Visible = false;
            this.pictureBox4.Visible = false;
        }

        private void button6_Click_1(object sender, EventArgs e)
        {//soso
            emotion_value = 2;
            this.emotion_pic_view.Image = global::DiaryPlanner.Properties.Resources.soso_big;
            this.button4.Visible = false;
            this.button5.Visible = false;
            this.button6.Visible = false;
            this.button7.Visible = false;
            this.pictureBox4.Visible = false;
        }

        private void button7_Click_1(object sender, EventArgs e)
        {//angry
            emotion_value = 3;
            this.emotion_pic_view.Image = global::DiaryPlanner.Properties.Resources.angry_big;
            this.button4.Visible = false;
            this.button5.Visible = false;
            this.button6.Visible = false;
            this.button7.Visible = false;
            this.pictureBox4.Visible = false;
        }
        //////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////
        ////////////////////사진 or 그리기 기능 추가시 그기능을 불러오기 위한 이벤트
        private void button2_Click(object sender, EventArgs e)
        {
            Diary.pic_draw_add picture_draw_add = new pic_draw_add(this);
            picture_draw_add.ShowDialog();
        }


        ///////////////////////////////////////////////////////
        private void save_db(string daily_diary_text, string date_text)
        {
            try //예외처리문
            {
                string connectionString = "provider=Microsoft.JET.OLEDB.4.0;" //연결하는 객체의 공급자명(확장자명)
                                        + "data source = "  //데이터 소스=(경로입력하라는거)
                                        + Application.StartupPath ///상대경로 읽어옴
                                        + @"\diaryplannerdb.mdb";//실제 데이터베이스 파일이름

                string commandString = "Select * from [diary]";//diary테이블에 있는 모든필드들을 모두 검색해라.

                OleDbDataAdapter DBAdapter ////DataSet을 채우고 데이터 소스를 업데이트하는 데 사용되는 데이터 명령 집합 및 데이터베이스 연결하게 하는 함수
                    = new OleDbDataAdapter(commandString, connectionString);//connectionString이라는 경로와 쿼리명령을 동적할당

                OleDbCommandBuilder myCommandBuilder ///연결된 데이터베이스에서 DataSet의 변경 내용을 조정하는 데 사용되는 단일 테이블 명령을 자동으로 생성합니다
                    = new OleDbCommandBuilder(DBAdapter);

                DataSet DS = new DataSet(); //DS라는 데이터 셋 동적할당

                DBAdapter.Fill(DS, "diary"); //DS라는 데이터셋에 새로운 diary 테이블에 추가 또는 갱신하겠다고 알림.

                
                DataTable aTable = DS.Tables["diary"];//diary테이블을 불러와 aTable을 새로 생성해서 저장
                DataRow newRow = aTable.NewRow();


                newRow["Ddate"] = date_text + UserProfile.Instance().userId;
                newRow["daily_diary_text"] = daily_diary_text;
                newRow["align"] = align_value;
                newRow["emotion"] = emotion_value;
                if(check_photoOrdraw == 1){
                    newRow["filename"] = all_date;
                    object_diary_main.check_photo_draw = 1;
                }
                aTable.Rows.Add(newRow);
                DBAdapter.Update(DS, "diary");
                
                //new_daily_diary_text.Text = date_text+  "               "+ aTable.Rows[0].ItemArray[0].ToString();
            }
            catch (DataException DE)
            {
                MessageBox.Show(DE.Message);
            }
            catch (Exception DE)
            {
                MessageBox.Show(DE.Message);
            }

        }
        private void weather_view_Click(object sender, EventArgs e)
        {
            this.sunny.Visible = true;
            this.cloud.Visible = true;
            this.rain.Visible = true;
            this.snow.Visible = true;
            this.pictureBox1.Visible = true;
        }

        private void sunny_Click(object sender, EventArgs e)
        {
            weather_value = 0;
            this.weather_view.Image = global::DiaryPlanner.Properties.Resources.serenity_big;
            this.sunny.Visible = false;
            this.cloud.Visible = false;
            this.rain.Visible = false;
            this.snow.Visible = false;
            this.pictureBox1.Visible = false;
        }

        private void cloud_Click(object sender, EventArgs e)
        {
            weather_value = 1;
            this.weather_view.Image = global::DiaryPlanner.Properties.Resources.cloud_big;
            this.sunny.Visible = false;
            this.cloud.Visible = false;
            this.rain.Visible = false;
            this.snow.Visible = false;
            this.pictureBox1.Visible = false;
        }

        private void rain_Click(object sender, EventArgs e)
        {
            weather_value = 2;
            this.weather_view.Image = global::DiaryPlanner.Properties.Resources.rain_big;
            this.sunny.Visible = false;
            this.cloud.Visible = false;
            this.rain.Visible = false;
            this.snow.Visible = false;
            this.pictureBox1.Visible = false;
        }

        private void snow_Click(object sender, EventArgs e)
        {
            weather_value = 3;
            this.weather_view.Image = global::DiaryPlanner.Properties.Resources.snow_big;
            this.sunny.Visible = false;
            this.cloud.Visible = false;
            this.rain.Visible = false;
            this.snow.Visible = false;
            this.pictureBox1.Visible = false;
        }

        
    }
}
